package edu.cmu.cs.task7.model;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import com.ebay.sdk.util.XmlUtil;

import com.ebay.sdk.ApiAccount;
import com.ebay.sdk.ApiContext;
import com.ebay.sdk.ApiCredential;
import com.ebay.sdk.CallRetry;
import com.ebay.sdk.util.SdkProxySelector;


//The Model initialize the static variables(especially apiContext) for other models.
//Research Model don't need parameter from this model.

public class Model {
	public static ApiContext apiContext=new ApiContext();
	private final String CONFIG_XML_NAME = "C:\\CMPT\\javaproject\\task7\\src\\Config.xml";
	
	public Model() throws Exception{
		 // Enable Call-Retry.
        CallRetry cr = new CallRetry();
        cr.setMaximumRetries(3);
        cr.setDelayTime(1000); // Wait for one second between each retry-call.
        String[] apiErrorCodes = new String[] {
            "10007", // "Internal error to the application."
            "931", // "Validation of the authentication token in API request failed."
            "521", // Test of Call-Retry: "The specified time window is invalid."
            "124" // Test of Call-Retry: "Developer name invalid."
        };
        cr.setTriggerApiErrorCodes(apiErrorCodes);

        // Set trigger exceptions for CallRetry.
        // Build a dummy SdkSoapException so that we can get its Class.
        java.lang.Class[] tcs = new java.lang.Class[] {
            com.ebay.sdk.SdkSoapException.class
        };
        cr.setTriggerExceptions(tcs);

        apiContext.setCallRetry(cr);
        apiContext.setTimeout(180000);
   	 
        this.loadConfiguration();
        
        // Add listener for token renewal event.
        //apiContext.getApiCredential().addTokenEventListener(new DemoTokenEventListener(this));
		
	}
	
	public void loadConfiguration() throws Exception
    {
      String xmlPath = CONFIG_XML_NAME;
      Document doc = XmlUtil.createDomByPathname(xmlPath);//getConfigXmlText()

      Node config = XmlUtil.getChildByName(doc, "Configuration");
      if( config == null )
        throw new Exception("<Configuration> was not found.");

      String s;

      //this.apiContext = new ApiContext();

      s = XmlUtil.getChildString(config, "ServerUrl").trim();
      this.apiContext.setApiServerUrl(s);

      s = XmlUtil.getChildString(config, "EpsServerUrl").trim();
      this.apiContext.setEpsServerUrl(s);
      
      s = XmlUtil.getChildString(config, "SignInUrl").trim();
      this.apiContext.setSignInUrl(s);

      ApiCredential apiCred = new ApiCredential();
      this.apiContext.setApiCredential(apiCred);

      ApiAccount ac = new ApiAccount();
      apiCred.setApiAccount(ac);

      //
      Node apiCredential = XmlUtil.getChildByName(config, "ApiCredential");
      s = XmlUtil.getChildString(apiCredential, "Developer");
      ac.setDeveloper(s);
      s = XmlUtil.getChildString(apiCredential, "Application");
      ac.setApplication(s);
      s = XmlUtil.getChildString(apiCredential, "Certificate");
      ac.setCertificate(s);

      Node eBayCredential = XmlUtil.getChildByName(config, "eBayCredential");

      s = XmlUtil.getChildString(eBayCredential, "Token");
      apiCred.seteBayToken(s.trim());  
      
      s=XmlUtil.getChildString(config,"RuName");
      if(s.length() > 0){
      	this.apiContext.setRuName(s);
      }
      
      s = XmlUtil.getChildString(config, "Timeout");
      if (s.length() > 0)
      {
        int timeout = Integer.parseInt(s);
        this.apiContext.setTimeout(timeout);
      }
      
      //proxy setting
      Node proxy = XmlUtil.getChildByName(config, "Proxy");
      
      if (proxy != null) {
  	    String host = XmlUtil.getChildString(proxy, "Host");
  	    String port = XmlUtil.getChildString(proxy, "Port");
  	    if (host.length() > 0 && port.length() > 0) {
  	    	SdkProxySelector ps = null;
  	    	String username = XmlUtil.getChildString(proxy, "Username");
  	    	String password = XmlUtil.getChildString(proxy, "Password");
  	    	if (username.length() > 0 && password.length() > 0) {
  	    		ps = new SdkProxySelector(host, Integer.parseInt(port), username, password);
  	    	} else {
  	    		ps = new SdkProxySelector(host, Integer.parseInt(port));
  	    	}
  	    	ps.register();
  	    }
      }
      
    }
	
	

}
